import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.io.*;
import java.util.Scanner;

public class file {
    static Scanner reader = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        System.out.println("欢迎来到文件管理系统");
        System.out.println("输入：1 目录改变 输入：2 文件改变");
        String option = reader.nextLine();
        switch (option) {
            case "1":
                directory();
                break;
            case "2":
                filee();
                break;
        }
    }
    static void directory() {
        System.out.println("输入 1：改变目录 2：显示目录 3：创建目录 4：删除目录");
        String opt2 = reader.nextLine();
        switch (opt2) {
            case "1":
                cd();
                break;
            case "2":
                dir();
                break;
            case "3":
                md();
                break;
            case "4":
                rd();
                break;
        }
    }

    static void filee() throws IOException {
        System.out.println("输入 1：新建文件 2：删除文件 3：退出文件");
        String opt3 = reader.nextLine();
        switch (opt3) {
            case "1":
                edit();
                break;
            case "2":
                del();
                break;
            case "3":
                exit();
                break;
        }
    }

    static void cd() {
        String directory=reader.nextLine();
        File path=new File(directory);
        if(path.exists()){
            String newdir=reader.nextLine();
            path.renameTo(new File(newdir));
        }
    }

    static void dir() {
        String fullname = reader.nextLine();
        File f = new File(fullname);
        tree(f,0);
    }
    static void tree(File f,int level){
        String preStr="";
        for(int i=0;i<level;i++){
            preStr+="    ";
        }
        File[] childs = f.listFiles();
        for(int i=0;i< childs.length;i++){
            System.out.println(preStr+childs[i].getName());
            if(childs[i].isDirectory()){
                tree(childs[i],level+1);
            }
        }
    }

    static void md() {
        String path = reader.nextLine();
        File file = new File(path);
        if(file.exists()) {
            System.out.println("文件夹已存在");
        }else {
            if(file.mkdir()) {
                System.out.println("文件夹创建成功");
            }else {
                System.out.println("文件夹创建失败");
            }
        }
    }

    static void rd() {
        String path = reader.nextLine();
        File file = new File(path);
        if(file.exists()) {
            file.delete();
            System.out.println("文件夹删除成功");
        }else {
            System.out.println("文件夹不存在");
        }
    }

    static void edit() {
        String filename = reader.nextLine();
        String directory = reader.nextLine();
        File f = new File(directory, filename);
        if (f.exists()) {
            System.out.println("文件名" + f.getAbsolutePath());
            System.out.println("文件大小" + f.length());
        } else {
            f.getParentFile().mkdirs();
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    static void del(){
        String filename = reader.nextLine();
        String directory = reader.nextLine();
        File f = new File(directory, filename);
        if(f.exists()) {
            f.delete();
        }else {
            System.out.println("没有找到文件");
        }
    }
    static void exit() throws IOException {
        String filename = reader.nextLine();
        String directory = reader.nextLine();
        String fullname=filename+directory;
        FileReader f = new FileReader(fullname);
        f.close();
    }
}

